package com.coder.rental.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.rental.entity.CarInfo;
import com.coder.rental.mapper.CarInfoMapper;
import com.coder.rental.service.ICarInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coder.rental.service.IOssService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 汽车信息 服务实现类
 * </p>
 *
 * @author zhaojj
 * @since 2025-01-10
 */
@Service
public class CarInfoServiceImpl extends ServiceImpl<CarInfoMapper, CarInfo> implements ICarInfoService {
    @Resource
    private IOssService ossService;
    @Override
    public Page<CarInfo> searchByPage(Page<CarInfo> page, CarInfo carInfo) {
        return baseMapper.searchByPage(page,carInfo);
    }

    @Override
    public boolean delete(String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::parseInt).toList();
        if(!list.isEmpty()){
            // 同时删除图片
            list.forEach(id->{
                CarInfo carInfo = baseMapper.selectById(id);
                if(carInfo!=null) {
                    if (carInfo.getPic() != null) {
                        // 删除时，要提示删除车辆的图片
                        ossService.delete(carInfo.getPic());
                    }
                }
            });
            return baseMapper.deleteBatchIds(list)>0;
        }
        return false;
    }

    @Override
    public CarInfo selectByCarNum(String carNum) {
        QueryWrapper<CarInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("car_num",carNum);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<CarInfo> toBeMaintain() {
        return baseMapper.toBeMaintain();
    }
}
